/*created by hp
*on 18-10-12 下午1:31
*/
package com.happts.UI;
import com.happts.Entity.USER;
import com.happts.Service.USERService;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class LoginUI{
    private JTextField jUsernameText;//定义文本框组件
    private JPasswordField jPasswordField;//定义密码框组件
    private JLabel labelUser, labelPass;
    private JPanel jp1,jp2,jp3;
    private JButton jb_login, jb_signin; //创建按钮
    private ImageIcon login = new ImageIcon("src/com/happts/img/login.png");
    private ImageIcon signin = new ImageIcon("src/com/happts/img/regist.png");
    public LoginUI() {
        JFrame frame = UtilView.createFrame("用户登录");
        jUsernameText = new JTextField(12);
        jPasswordField = new JPasswordField(12);
        labelUser = new JLabel("用户名");
        labelPass = new JLabel("密 码");

        jb_signin = new JButton(signin);
        jb_login = new JButton(login);


        //设置布局
        frame.setLayout(null);
//        frame.setLayout(new GridLayout(3, 2));
        frame.setSize(400, 230);
        frame.setLocation(750, 300);
        int x=50, y=50, w1=30, w2=200, h1= 30;
        labelUser.setBounds(x, y, x, h1);
        jUsernameText.setBounds(x+x+20, y, w2, h1);
        labelPass.setBounds(x, y+h1+10, x, h1);
        jPasswordField.setBounds(x+x+20, y+h1+10, w2-w1-5, h1);

        jb_signin.setBounds(x+x+20+w2-w1, y+h1+10, 30, 30);
        jb_login.setBounds(80, 150, 242, 37);


        frame.add(labelUser);
        frame.add(jUsernameText);//第一块面板添加用户名和文本框

        frame.add(labelPass);
        frame.add(jPasswordField);//第二块面板添加密码和密码输入框

        frame.add(jb_login);
        frame.add(jb_signin); //第三块面板添加确认和取消

        //        pp3.setLayout(new FlowLayout());  　　//因为JPanel默认布局方式为FlowLayout，所以可以注销这段代码.

        //设置显示

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        frame.setTitle("登陆");

        jb_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = jUsernameText.getText().trim();
                String password = jPasswordField.getText();
//                USER user = new USER(username, password);
                USERService us = new USERService();
                List<USER> userList = us.find(username);
                if(userList.get(0).getPassword().equals(password)){
                    frame.dispose();
                    JOptionPane.showMessageDialog(frame, "你好，"+username);

                    //界面跳转
                    new Menu();

                }else{
                    JOptionPane.showMessageDialog(frame,
                            "密码错误",
                            "登录失败",
                            JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        jb_signin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                frame.dispose();
                new SigninUI();
            }
        });

    }
    public static void main(String[] args){
        new LoginUI();
    }
}
